package com.lifo.utils.common.messagehandler;

import com.lifo.utils.common.json.JsonUtil;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * @author why
 * @date 2017/7/24 0024
 */
@Slf4j
public class JsonStandardMsgBean implements Serializable {

    private static final String RESULT = "result";
    private static JsonUtil wrapperJson = new JsonUtil();
    /**
     * 0,1....等数字常量标志
     */
    private static final int ZERO = 0;
    private static final int ONE = 1;

    /**
     * 错误码
     */
    private String resultCode;

    /**
     * 错误信息
     */
    private String resultMsg;

    /**
     * 业务数据
     */
    private Object body;

    public String getResultCode() {
        return resultCode;
    }

    public void setResultCode(String resultCode) {
        this.resultCode = resultCode;
    }

    public String getResultMsg() {
        return resultMsg;
    }

    public void setResultMsg(String resultMsg) {
        this.resultMsg = resultMsg;
    }

    public Object getBody() {
        return body;
    }

    public void setBody(Object body) {
        this.body = body;
    }

    public static void main(String[] args) {
        //System.out.println(gson.toJson(ResponseMsgBean.getMsgJson("sdfsdf","adfadsf",new Object())));
    }

    public JsonStandardMsgBean(String resultCode, String resultMsg, Object body) {
        this.resultCode = resultCode;
        this.resultMsg = resultMsg;
        if (StringUtils.isEmpty(body)) {
            this.body = new Object();
        } else {
            this.body = body;
        }
    }

    public JsonStandardMsgBean(String resultCode, String resultMsg) {
        this.resultCode = resultCode;
        this.resultMsg = resultMsg;
        this.body = new Object();
    }

    /**
     * 自定义消息内容，消息码，包含业务数据
     *
     * @param resultCode 消息代码 数字
     * @param resultMsg  消息内容  汉字或英文
     * @param body       业务数据(可选项，为空null)
     * @return
     */
    public static String getMsgJson(@NonNull String resultCode, @NonNull String resultMsg, Object body) {
        JsonStandardMsgBean msgBean = new JsonStandardMsgBean(resultCode, resultMsg, body);
        Map map = new HashMap(1);
        map.put(RESULT, msgBean);
        return wrapperJson.toJson(map);
    }


    /**
     * 自定义消息内容，消息码，不包含业务数据
     *
     * @param resultCode 消息代码  数字
     * @param resultMsg  消息内容 汉字或英文
     * @return
     */
    public static String getMsgJson(@NonNull String resultCode, @NonNull String resultMsg) {
        JsonStandardMsgBean msgBean = new JsonStandardMsgBean(resultCode, resultMsg);
        Map map = new HashMap(1);
        map.put(RESULT, msgBean);
        return wrapperJson.toJson(map);
    }

    /**
     * 请求参数或格式错误
     *
     * @return
     */
    public static String getMsgJsonParamError() {
        JsonStandardMsgBean msgBean = new JsonStandardMsgBean(DefaultMsgResult.PARAMS_ERROR.getCode(), DefaultMsgResult.PARAMS_ERROR.getMsg());
        Map map = new HashMap(1);
        map.put(RESULT, msgBean);
        return wrapperJson.toJson(map);
    }

    /**
     * 操作成功，返回业务数据
     *
     * @param body 业务数据
     * @return
     */
    public static String getMsgJsonWithData(@NonNull Object body) {
        JsonStandardMsgBean msgBean = new JsonStandardMsgBean(DefaultMsgResult.SUCCESS.getCode(), DefaultMsgResult.SUCCESS.getMsg(), body);
        Map map = new HashMap(1);
        map.put(RESULT, msgBean);
        return wrapperJson.toJson(map);
    }

    /**
     * 系统错误，系统异常
     *
     * @return
     */
    public static String getMsgJsonWithSystemError() {
        JsonStandardMsgBean msgBean = new JsonStandardMsgBean(DefaultMsgResult.SYSTEM_ERROR.getCode(), DefaultMsgResult.SYSTEM_ERROR.getMsg());
        Map map = new HashMap(1);
        map.put(RESULT, msgBean);
        return wrapperJson.toJson(map);
    }

    /**
     * 操作成功，不需要返回业务数据
     *
     * @return
     */
    public static String getMsgJsonWithSuccess() {
        JsonStandardMsgBean msgBean = new JsonStandardMsgBean(DefaultMsgResult.SUCCESS.getCode(), DefaultMsgResult.SUCCESS.getMsg());
        Map map = new HashMap(1);
        map.put(RESULT, msgBean);
        return wrapperJson.toJson(map);
    }


    /**
     * 查询成功，需要返回业务数据，但是未找到想要的数据
     *
     * @return
     */
    public static String getMsgJsonWithoutData() {
        JsonStandardMsgBean msgBean = new JsonStandardMsgBean(DefaultMsgResult.NODATA.getCode(), DefaultMsgResult.NODATA.getMsg());
        Map map = new HashMap(1);
        map.put(RESULT, msgBean);
        return wrapperJson.toJson(map);
    }


    /**
     * 返回标准Json信息
     * 1、提示信息“查询成功，没有获取到你想要的信息”
     * 2、提示信息“操作成功”，body存有业务数据
     * @param t
     * @return
     */
    public static String getMsgJsonDataOrNotData(Object t)  {
        StringBuffer resultStr = new StringBuffer(JsonStandardMsgBean.getMsgJsonWithoutData());
        if (!StringUtils.isEmpty(t)){
            resultStr.setLength(ZERO);
            resultStr.append(JsonStandardMsgBean.getMsgJsonWithData(t));
        }
        return resultStr.toString();
    }


}